﻿##Vanilla##
#Can marry right now
can_marry_trigger = {
	can_marry_common_trigger = yes
	allowed_more_spouses = yes
	NOT = { 
		has_character_modifier = mourner_modifier 
	}
	#To account for a person marrying their concubines please use can_marry_character_trigger
}

make_concubine_is_valid_trigger = {
	custom_description = {
		text = take_concubine_allowed_more
		object = scope:recipient
		scope:actor = {
			allowed_more_concubines = yes
		}
	}
	scope:recipient = {
		is_busy_in_events_localised = yes
		custom_tooltip = {
			text = take_concubine_mourning
			OR = {
				AND = {
					is_imprisoned_by = scope:actor
					is_married = yes
					OR = {
						NOT = { 
							has_character_modifier = mourner_modifier 
						}
						AND = {
							has_character_modifier = mourner_modifier 
							NOT = {
								is_courtier_of = scope:actor
							}
						}
					}
				}
				AND = {
					is_married = no
					OR = {
						NOT = { 
							has_character_modifier = mourner_modifier 
						}
						AND = {
							has_character_modifier = mourner_modifier 
							NOR = { #So that you won't take any of your parent's widows as your concubines right away
								any_former_spouse = {
									even_if_dead = yes
									any_child = {
										this = scope:actor
									}
								}
								any_former_concubinist = {
									even_if_dead = yes
									any_child = {
										this = scope:actor
									}
								}
							}
						}
					}
				}
			}
		}
		is_hostage = no
		can_be_taken_as_muslim_concubine_trigger = { 
			CHARACTER = scope:actor 
			RECEIVER = scope:actor
		}
	}
}

can_become_concubine_of_character_trigger = {
	is_hostage = no
	trigger_if = {
		limit = {
			NOT = { is_imprisoned_by = $CHARACTER$ }
		}
		is_married = no
		is_betrothed = no
		is_concubine = no
		number_of_concubines = 0
		OR = {
			NOT = { 
				has_character_modifier = mourner_modifier 
			}
			AND = {
				has_character_modifier = mourner_modifier 
				NOR = {
					any_former_spouse = {
						even_if_dead = yes
						any_child = {
							this = $CHARACTER$
						}
					}
					any_former_concubinist = {
						even_if_dead = yes
						any_child = {
							this = $CHARACTER$
						}
					}
				}
			}
		}
	}
	trigger_if = {
		limit = {
			is_imprisoned_by = $CHARACTER$
			is_married = yes
		}
		OR = {
			NOT = { 
				has_character_modifier = mourner_modifier 
			}
			AND = {
				has_character_modifier = mourner_modifier 
				NOR = {
					any_former_spouse = {
						even_if_dead = yes
						any_child = {
							this = $CHARACTER$
						}
					}
					any_former_concubinist = {
						even_if_dead = yes
						any_child = {
							this = $CHARACTER$
						}
					}
				}
			}
		}
		NOT = { is_spouse_of = $CHARACTER$ }
	}
	NOR = {
		is_concubine_of = $CHARACTER$
		has_character_flag = has_been_offered_as_concubine
	}
	is_ruler = no
	is_playable_character = no
	is_adult = yes
	could_marry_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, recent divorce, allowed to marry, no illegal incest etc.
}

can_be_offered_as_concubine_to_character_trigger = {
	trigger_if = {
		limit = {
			NOT = { is_imprisoned_by = $GIVER$ }
		}
		is_married = no
		is_betrothed = no
		OR = {
			is_concubine = no
			is_concubine_of = $GIVER$
		}
		number_of_concubines = 0
	}
	trigger_if = {
		limit = {
			is_imprisoned_by = $GIVER$
			is_married = yes
		}
		NOR = {
			is_spouse_of = $CHARACTER$
			is_spouse_of = $GIVER$
		}
	}
	NOR = {
		is_concubine_of = $CHARACTER$
		has_character_flag = has_been_offered_as_concubine
	}
	is_ruler = no
	is_playable_character = no
	is_adult = yes
	NOT = {
		is_theocratic_lessee = yes
	}
	could_marry_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, recent divorce, allowed to marry, no illegal incest etc.
	can_be_taken_as_muslim_concubine_trigger = { # Can a Muslim be taken as a concubine?
		CHARACTER = $GIVER$
		RECEIVER = $CHARACTER$
	}
}

valid_marriage_legitimacy_rank_trigger = {
	# ROOT = character being checked (either OTHER or their relative)
	# RULER = character getting legitimacy
	
	is_landed = yes
	OR = {
		# Outranks RULER
		tier_difference = {
			target = $RULER$
			value > 0
		}
		# Same rank
		tier_difference = {
			target = $RULER$
			value = 0
		}
		# Special case: King/Emperor for Emperor+ (including future hegemony)
		AND = {
			OR = {
				highest_held_title_tier = tier_kingdom
				highest_held_title_tier = tier_empire
			}
			$RULER$ = {
				highest_held_title_tier >= tier_empire
			}
		}
	}
}

can_be_taken_as_muslim_concubine_trigger = {
	# CHARACTER = the actor, either taking the concubine or offering them
	# RECEIVER = the character being offered the concubine, if they exist
	custom_tooltip = {
		text = take_muslim_concubine_trigger
		trigger_if = {
			limit = {
				religion = religion:islam_religion
			}
			OR = {
				is_imprisoned_by = $CHARACTER$ # If they are imprisoned, they kinda are slaves already
				AND = {
					is_concubine_of = $CHARACTER$ # To send concubines to fellow Muslims
					$RECEIVER$ != $CHARACTER$
				}
				$CHARACTER$ = {
					trigger_if = {
						limit = {
							religion = religion:islam_religion
						}
						has_trait = cynical
					}
					trigger_else = {
						NOT = {
							religion = religion:islam_religion
						}
					}
				}
				has_character_flag = abducted_non_believer
			}
			trigger_if = { # Offering a concubine to a fellow Muslim
				limit = {
					$RECEIVER$ != $CHARACTER$
				}
				trigger_if = { # If the concubine isn't a slave, the receiver can't be a Muslim, or they're cynical
					limit = {
						is_imprisoned = no
						is_concubine = no
						NOT = { has_character_flag = abducted_non_believer }
					}
					$RECEIVER$ = {
						trigger_if = {
							limit = {
								religion = religion:islam_religion
							}
							has_trait = cynical
						}
						trigger_else = {
							NOT = {
								religion = religion:islam_religion
							}
						}
					}
				}
			}
		}
		trigger_else = {
			NOT = {
				religion = religion:islam_religion
			}
		}
	}
	# You cannot offer your children's parents
	trigger_if = {
		limit = {
			$CHARACTER$ != $RECEIVER$
		}
		trigger_if = { # Prevent the AI from offering potential heirs
			limit = {
				has_trait = pregnant
				exists = pregnancy_assumed_father
			}
			NOT = { pregnancy_assumed_father = $CHARACTER$ }
		}
		trigger_if = { # After having a child, a concubine can't be given away
			limit = {
				$CHARACTER$.religion = religion:islam_religion
			}
			NOT = {
				any_child = {
					even_if_dead = yes
					is_child_of = $CHARACTER$
					count > 0
				}
			}
		}
	}
	custom_tooltip = {
		text = take_concubine_ex_spouse_faith
		trigger_if = { # You can't take a freed person as a concubine
			limit = {
				$CHARACTER$.religion = religion:islam_religion
			}
			NOR = {
				is_spouse_of = $CHARACTER$
				has_divorced_opinion_trigger = {
					TARGET = $CHARACTER$
				}
				any_former_spouse = {
					this = $CHARACTER$
				}
			}
		}
	}
}
